//
//  GMToast.m
//  GomeEShop
//
//  Created by hu xuesen on 13-3-19.
//  Copyright (c) 2013年 Gome. All rights reserved.
//

#import "GMToast.h"

@implementation GMToast

#pragma mark - 屏幕的高和宽，物理宽度
#define screenH   ([UIScreen mainScreen].bounds.size.height)
#define screenW   ([UIScreen mainScreen].bounds.size.width)

#define GMK_NavBar_H (64)        /// NavBar的高度

#define GMM_Toast_Min_W (120) // toast 宽度最小值
#define GMM_Toast_Max_W (200) // toast 宽度最大值
#define GMM_Toast_Max_H (screenH-GMK_NavBar_H) // toast 高度最大值

@synthesize textLable;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code uatmhl6  qq123456
    }
    return self;
}

- (id)initWithView:(UIView*)view text:(NSString*)text duration:(float)inDuration
{
    int width = GMM_Toast_Min_W;
    int height = GMM_Toast_Max_H;
    /// 弃用
//    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:15]];
    NSDictionary *dicAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:15]};
    CGSize size = [text sizeWithAttributes:dicAttributes];
    
    if (size.width > width-20)
    {
        if (size.width < GMM_Toast_Max_W-20)
        {
            width = ceilf(size.width)+20;
            height = ceilf(size.height)+20;
        }
        else
        {
            width = GMM_Toast_Max_W;
          
            /// 弃用
//            CGSize size1 = [text sizeWithFont:[UIFont systemFontOfSize:15]
//                           constrainedToSize:CGSizeMake(width-20, MAXFLOAT)
//                               lineBreakMode:NSLineBreakByCharWrapping];
            
            CGSize size1 = [text boundingRectWithSize:CGSizeMake(width-20, MAXFLOAT)
                                              options:NSStringDrawingTruncatesLastVisibleLine
                                           attributes:dicAttributes context:nil].size;
            
            if (size1.height < GMM_Toast_Max_H-20)
            {
                height = ceilf(size1.height) + 20;
            }
        }
    }
    else
    {
        width = ceilf(size.width)+20;
        height = ceilf(size.height)+20;
    }
    
    CGRect rect = CGRectMake((view.bounds.size.width-width)/2, (view.bounds.size.height-height)/2, width, height);
    self = [super initWithFrame:rect];
    if (self) {
        // Initialization code
        self.layer.cornerRadius = 6;
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
        self.alpha = 0;
        
        duration = inDuration;
        
        textLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.bounds.size.width-20, height-20)];
        textLable.backgroundColor = [UIColor clearColor];
        textLable.font = [UIFont systemFontOfSize:15];
        textLable.textColor = [UIColor whiteColor];
        textLable.textAlignment = NSTextAlignmentCenter;
        textLable.numberOfLines = 0;
        textLable.lineBreakMode = NSLineBreakByCharWrapping;
        textLable.text = text;
        [self addSubview:textLable];
        
        [view addSubview:self];
    }
    return self;
}

- (void)show {    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1;
    }];
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(closeToast) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];    
}

- (void)closeToast{
	[UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
